package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.home.beans.CoachidleTimeContentBean;
import com.hongyuan.sellfitness.ui.home.beans.GroupClassTopBean;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CoachIdleTimeViewModel extends CustomViewModel<MyRepository> {

    //当前选中项
    private int mSelectPosition = 0;
    private List<GroupClassTopBean> mList;

    public CoachIdleTimeViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<CoachIldeTiemTopItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<CoachIldeTiemTopItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_coach_idletime_top);

    //给RecyclerView添加ObservableList
    public ObservableList<CoachIdleTimeItmeViewModel> contentList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<CoachIdleTimeItmeViewModel> contentItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_coach_idletime);

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_PLAN_DATE){
            mList = (List<GroupClassTopBean>) dataBean;
            for(int i = 0 ; i < mList.size() ; i++){
                if(i == 0){
                    mList.get(i).setSelect(true);
                }
                CoachIldeTiemTopItemViewModel itemViewModel = new CoachIldeTiemTopItemViewModel(this,mList.get(i));
                observableList.add(itemViewModel);
            }

            //获取空闲时间表
            reqTimes(mList.get(0).getNow_day());
        }

        if(code == Constants.GET_COACH_TIMEPLAN_DAY_LIST){
            List<CoachidleTimeContentBean> list = (List<CoachidleTimeContentBean>) dataBean;

            for(CoachidleTimeContentBean bean : list){
                CoachIdleTimeItmeViewModel itmeViewModel = new CoachIdleTimeItmeViewModel(this,bean);
                contentList.add(itmeViewModel);
            }
        }
    }

    /*
    * 请求教练时间列表
    * */
    private void reqTimes(String days){
        contentList.clear();

        setParams("day",days);
        requestData(Constants.GET_COACH_TIMEPLAN_DAY_LIST);
    }

    /*
     * 刷新下列表
     * */
    public void changeList(CoachIldeTiemTopItemViewModel itemViewModel){

        int newSelectPosition = getItemPosition(itemViewModel);
        if(newSelectPosition != mSelectPosition){
            //先改变选中项
            mList.get(mSelectPosition).setSelect(false);
            CoachIldeTiemTopItemViewModel oldSelect = new CoachIldeTiemTopItemViewModel(this,mList.get(mSelectPosition));
            observableList.set(mSelectPosition,oldSelect);

            mSelectPosition = getItemPosition(itemViewModel);
            mList.get(mSelectPosition).setSelect(true);
            CoachIldeTiemTopItemViewModel newSelect = new CoachIldeTiemTopItemViewModel(this,mList.get(mSelectPosition));
            observableList.set(mSelectPosition,newSelect);

            //改变设定为第一页
            curPage = FIRST_PAGE;
            //请求一次
            reqTimes(mList.get(mSelectPosition).getNow_day());
        }

    }

    /**
     * 获取条目下标
     *
     * @param itemViewModel
     * @return
     */
    public int getItemPosition(CoachIldeTiemTopItemViewModel itemViewModel) {
        return observableList.indexOf(itemViewModel);
    }
}
